package serv;

import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

public class GwkOrgan {
	public Vector<Vector<String>> m_organ = new Vector<Vector<String>>();
	private Vector<Vector<String>> m_porgan = new Vector<Vector<String>>();
	String m_USR_ID = "";
	String m_USR_NAME = "";
	String m_BANK_ORGAN_ID = "";
	String m_BANK_ORGAN_NAME = "";
	String bank_organ_level = "";
	String close="";
	String manager="";
	String today=null;		//经常用到当日日期，故加上
	private String m_BANK_PARENT_ID="";
	
	public GwkOrgan(){
	}
	
	/**   
	 * close   
	 *   
	 * @return the close   
	 * @since   CodingExample Ver(编码范例查看) 1.0   
	 */
	public String getClose() {
		return close;
	}

	/**   
	 * manager   
	 *   
	 * @return the manager   
	 * @since   CodingExample Ver(编码范例查看) 1.0   
	 */
	public String getManager() {
		return manager;
	}

	/**   
	 * bank_organ_level   
	 *   
	 * @return the bank_organ_level   
	 * @since   CodingExample Ver(编码范例查看) 1.0   
	 */
	public String getBank_organ_level() {
		return bank_organ_level;
	}

	/**   
	 * today   
	 *   
	 * @return the today   
	 * @since   CodingExample Ver(编码范例查看) 1.0   
	 */
	public String getToday() {
		if(today==null){
			SimpleDateFormat df1 = new SimpleDateFormat("yyyyMMdd");
			today = df1.format(new Date()).toString();
		}
		return today;
	}

	/**   
	 * m_USR_ID   
	 *   
	 * @return the m_USR_ID   
	 * @since   CodingExample Ver(编码范例查看) 1.0   
	 */
	public String getM_USR_ID() {
		return m_USR_ID;
	}

	/**   
	 * m_USR_NAME   
	 *   
	 * @return the m_USR_NAME   
	 * @since   CodingExample Ver(编码范例查看) 1.0   
	 */
	public String getM_USR_NAME() {
		return m_USR_NAME;
	}

	/**   
	 * m_BANK_ORGAN_ID   
	 *   
	 * @return the m_BANK_ORGAN_ID   
	 * @since   CodingExample Ver(编码范例查看) 1.0   
	 */
	public String getM_BANK_ORGAN_ID() {
		return m_BANK_ORGAN_ID;
	}

	/**   
	 * m_BANK_ORGAN_NAME   
	 *   
	 * @return the m_BANK_ORGAN_NAME   
	 * @since   CodingExample Ver(编码范例查看) 1.0   
	 */
	public String getM_BANK_ORGAN_NAME() {
		return m_BANK_ORGAN_NAME;
	}

	/**   
	 * 此方法描述的是： 根据用户，获取自己机构
	 * @author: 朱凯锋   
	 * @version: 2013年8月13日 下午2:50:09   
	 * @return: -1,manager 0,success
	 */
	public int GetMyOrgan(String User) throws SQLException, IOException{
		m_USR_ID = User;
		if(User.equalsIgnoreCase("manager")){
			m_USR_NAME = "超级管理员";
			m_BANK_ORGAN_ID = "manager";
			m_BANK_ORGAN_NAME = "管理机构";
			return -1;
		}
		String mysql = "select a.USR_NAME,b.BANK_ORGAN_ID,b.BANK_PARENT_ID,b.BANK_ORGAN_NAME," + 
				"b.BANK_ORGAN_LEVEL,a.manager,a.close " + 
				" from GX_CORP_USR a, GX_CORP_BANK_ORGAN b "+
				"where a.BANK_ORGAN_ID = b.BANK_ORGAN_ID " + 
				"and a.USR_ID = ";
		mysql += "\'" + User +"\' ";
		GwkDB gwkdb = new GwkDB();
		int ret = gwkdb.query(mysql, 7);
		if(ret<0)
			return ret;
		m_organ.clear();
		m_organ = gwkdb.dbvt;
		if(m_organ.size()>0){
			m_USR_NAME = m_organ.get(0).get(0).toString();
			m_BANK_ORGAN_ID = m_organ.get(0).get(1).toString();
			m_BANK_PARENT_ID = m_organ.get(0).get(2).toString();
			m_BANK_ORGAN_NAME = m_organ.get(0).get(3).toString();
			bank_organ_level = m_organ.get(0).get(4).toString();
			manager = m_organ.get(0).get(5).toString();
			close = m_organ.get(0).get(6).toString();
		}else
			return -1;
		return 0;
	}
	/**   
	 * 此方法描述的是： 获取下一级机构
	 * @author: 朱凯锋   
	 * @version: 2013年8月13日 下午2:50:38   
	 */
	public int GetSubOrgan(String BANK_ORGAN_ID) throws SQLException, IOException{
		String[] Organ_id = new String[1];
		Organ_id[0]=BANK_ORGAN_ID;
		int ret = PGetSubOrgan(Organ_id);
		if(ret==0){
			m_organ.clear();
			for(int i=0;i<m_porgan.size();i++){
				m_organ.add(m_porgan.get(i));
			}
		}
		return ret;
	}
	/**   
	 * 此方法描述的是： 获取BANK_ORGAN_ID，所有隶属机构号
	 * @author: 朱凯锋   
	 * @version: 2013年8月13日 下午2:41:36   
	 */
	public int GetOwnOrgan(String BANK_ORGAN_ID) throws SQLException, IOException{
		int ret = GetSubOrgan(BANK_ORGAN_ID);
		if(ret<0){
			return ret;
		}

		while(m_porgan.size()>0){
			String[] li_organ = new String[m_porgan.size()];
			for(int i=0;i<m_porgan.size();i++){
				li_organ[i] = m_porgan.get(i).get(0).toString();
			}
			ret = PGetSubOrgan(li_organ);
			if(ret==0){
				for(int j=0;j<m_porgan.size();j++){
					m_organ.add(m_porgan.get(j));
				}
			}else
				break;
		}
		//补上自己的机构号
		Vector<String> vttmp = new Vector<String>();
		if(m_BANK_ORGAN_ID.length()>0)
			vttmp.add(m_BANK_ORGAN_ID);
		else
			vttmp.add(BANK_ORGAN_ID);
		vttmp.add(m_BANK_PARENT_ID);
		vttmp.add(m_BANK_ORGAN_NAME);
		m_organ.add(0,vttmp);
	
		return 0;
	}
	private int PGetSubOrgan(String[] BANK_ORGAN_ID) throws SQLException, IOException{
		String li_id = "";
		for(int i=0;i<BANK_ORGAN_ID.length;i++){
			if(i!=0){
				li_id +=",";
			}
			li_id += "\'" + BANK_ORGAN_ID[i] + "\'";
		}
		String mysql = "select BANK_ORGAN_ID,BANK_PARENT_ID,BANK_ORGAN_NAME" + 
				" from GX_CORP_BANK_ORGAN "+
				"where BANK_PARENT_ID in (";
		mysql += li_id +") ";
		GwkDB gwkdb = new GwkDB();
		int ret = gwkdb.query(mysql, 3);
		if(ret<0)
			return ret;
		m_porgan = gwkdb.dbvt;
		return 0;
	}
	public String GetPwd(String User) throws SQLException, IOException{
		String mysql = "select USR_PWD from GX_CORP_USR "+
				"where USR_ID = ";
		mysql += "\'" + User +"\' ";
		GwkDB gwkdb = new GwkDB();
		int ret = gwkdb.query(mysql, 1);
		if(ret<0)
			return null;
		
		if(gwkdb.dbvt.size()>0)
			return gwkdb.dbvt.get(0).get(0).toString();
		else
			return null;
	}
	public void GetAllOrgan() throws SQLException, IOException{
		//use for manager
		String mysql = "select BANK_ORGAN_ID,BANK_PARENT_ID,BANK_ORGAN_NAME" + 
				" from GX_CORP_BANK_ORGAN ";
		GwkDB gwkdb = new GwkDB();
		int ret = gwkdb.query(mysql, 3);
		if(ret<0)
			return;
		m_organ = gwkdb.dbvt;
	}
}
